import {message} from "antd";
import UserQuery from "../../components/user/UserQuery";
import {useEffect, useState} from "react";
import RequestUtils from "../../utils/RequestUtils";
import Apis from "../../configs/Apis";
import UserList from "../../components/user/UserList";

const User = () => {

    const [messageApi, contextHolder] = message.useMessage();

    // 分页查询用户条件
    const [pageQueryUserReq, setPageQueryUserReq] = useState({
        pageNum: 1,
        pageSize: 10,
        userAccount: "", //用户账号
        userNickname: "" //用户昵称
    });

    // 分页查询用户返回结果
    const [pageQueryUserResp, setPageQueryUserResp] = useState({
        total: 0, //总条数
        pageCount: 1, //总页数
        records: [] //用户列表
    })

    useEffect(() => {
        // 页面打开后先进行一次分页查询
        doPageQuerySysUsers();
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);

    /**
     * 分页查询用户信息
     * @param param 传入的查询参数
     */
    const doPageQuerySysUsers = (param) => {

        const pageQueryParam = param ? {
            pageNum: param.pageNum,
            pageSize: param.pageSize,
            userAccount: param.userAccount,
            userNickname: param.userNickname
        } : {
            ...pageQueryUserReq
        }

        RequestUtils.post(Apis.user.pageQueryUser, {
            pageNum: pageQueryParam.pageNum,
            pageSize: pageQueryParam.pageSize,
            userAccount: pageQueryParam.userAccount,
            userNickname: pageQueryParam.userNickname
        }).then(res => {
            setPageQueryUserResp({
                total: res.total,
                pageCount: res.pageCount,
                records: res.records
            })
        }).catch(err => {
            messageApi.error("查询用户失败: " + err.message);
        })
    }

    return (
        <div>
            {contextHolder}

            {/* 页面查询条件部分 */}
            <UserQuery
                pageQueryUserReq={pageQueryUserReq}
                setPageQueryUserReq={setPageQueryUserReq}
                doPageQuerySysUsers={doPageQuerySysUsers}
            />

            {/* 用户列表展示部分 */}
            <UserList
                pageQueryUserReq={pageQueryUserReq}
                setPageQueryUserReq={setPageQueryUserReq}
                pageQueryUserResp={pageQueryUserResp}
                doPageQuerySysUsers={doPageQuerySysUsers}
            />

        </div>
    )
}
export default User;